package com.exploadingelephants.engine;

import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;

/**
 * When the view is ready, this callback will handle the rest
 */
public class PlayViewCallback implements Callback {
	/** Main playground */
	private PlaygroundActivity playgroundActivity;
	
	/**
	 * @param playLoopThread
	 */
	public PlayViewCallback(PlaygroundActivity playgroundActivity, PlayLoopThread playLoopThread) {
		this.playgroundActivity = playgroundActivity;
	}

	/**
	 * When everything is ready, we can start the main game loop 
	 */
	public void surfaceCreated(SurfaceHolder holder) {
		playgroundActivity.onViewReady();
	}

	/**
	 * When the view is destroyed, we will also stop the main game loop
	 */
	public void surfaceDestroyed(SurfaceHolder holder) {
		boolean retry = true;
		playgroundActivity.getPlayLoopThread().stopLoop();

		while (retry) {
			// We will try until the main game loop will be stopped
			try {
				playgroundActivity.getPlayLoopThread().join();
				retry = false;
			} catch (InterruptedException e) {}
		}
	}
	
	/**
	 * Might consider to change  x/y axis when format changes..
	 */
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
	}
}
